package com.tally.user.controller;

import com.tally.server.dto.ResponseDTO;
import com.tally.server.exception.BusinessException;
import com.tally.server.exception.ValidatorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author: sy
 * @Date: 2020/11/14 21:03
 * @Description:
 */
@ControllerAdvice
public class ControllerExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(ControllerExceptionHandler.class);

    @ResponseBody
    @ExceptionHandler(value = ValidatorException.class)
    public ResponseDTO validatorExceptionHandler(ValidatorException e){
        logger.warn(e.getMessage());
        return ResponseDTO.fail("请求参数异常！");
    }

    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public ResponseDTO businessExceptionHandler(BusinessException e){
        logger.error("业务异常：{}",e.getCode().getDesc());
        return ResponseDTO.fail(e.getCode().getDesc());
    }
}
